package com.work.biz.controller;

import java.util.List;

import com.work.mp.service.impl.MpTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.work.common.annotation.Log;
import com.work.common.core.controller.BaseController;
import com.work.common.core.domain.AjaxResult;
import com.work.common.enums.BusinessType;
import com.work.biz.domain.BizSchool;
import com.work.biz.service.IBizSchoolService;

/**
 * 学校Controller
 *
 * @author lee
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/api/biz/school")
@Api(value = "学校api", tags = {"添加学校,学校查询接口"})
public class BizSchoolController extends BaseController {
    @Autowired
    private IBizSchoolService bizSchoolService;

    @Autowired
    private MpTokenService tokenService;

    /**
     * 查询学校列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询用户管理的所有学校", httpMethod = "GET", notes = "成功：0，验证失败：500")
    public AjaxResult list(@ApiParam(value = "用户token", required = true)
                           @RequestHeader("tk")
                                   String tk) {
        BizSchool bizSchool = new BizSchool();
        bizSchool.setUserId(tokenService.getUserId(tk));
        List<BizSchool> list = bizSchoolService.selectBizSchoolList(bizSchool);
        return AjaxResult.success(list);
    }

    /**
     * 获取学校详细信息
     */
    @GetMapping(value = "/{schoolId}")
    public AjaxResult getInfo(@PathVariable("schoolId") Long schoolId) {
        return AjaxResult.success(bizSchoolService.selectBizSchoolById(schoolId));
    }

    /**
     * 新增学校
     */
    @Log(title = "学校", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增学校", httpMethod = "POST", notes = "成功：0，验证失败：500")
    @PostMapping("/add")
    public AjaxResult add(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk,
            @ApiParam(value = "创建学校的参数", required = true)
            @RequestBody BizSchool bizSchool) {
        bizSchool.setUserId(tokenService.getUserId(tk));
        return toAjax(bizSchoolService.insertBizSchool(bizSchool));
    }

    /**
     * 修改学校
     */
    @Log(title = "学校", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizSchool bizSchool) {
        return toAjax(bizSchoolService.updateBizSchool(bizSchool));
    }

    /**
     * 删除学校
     */
    @Log(title = "学校", businessType = BusinessType.DELETE)
    @DeleteMapping("/{schoolIds}")
    public AjaxResult remove(@PathVariable Long[] schoolIds) {
        return toAjax(bizSchoolService.deleteBizSchoolByIds(schoolIds));
    }
}
